/**
 * 
 */
var rowNumber;
var pageSize;
var pageNumber;
$(function(){
	   $('#address_table')
		.datagrid(
				{
					url : 'admin/queryReceiverAddress.do?pageSize=10',
					collapsible : true,
					width : '100%',
					height :'450px',
					iconCls : 'icon-user',
					fitColumns : true,
					pagination : true,
					rownumbers : true,
					singleSelect:true,
					checkOnSelect:false,
					selectOnCheck:false,
					loadMsg:'正在加载数据',
					loadFilter:function(data){
						for(var i=0;i<data.rows.length;i++){
							if(data.rows[i].raIsAcquiescent){
								data.rows[i].raIsAcquiescent='是';
							}else{
								data.rows[i].raIsAcquiescent='否';
							}
						}
						 return data;
					},
					onClickRow: function(rowIndex, rowData) {
						rowNumber=rowIndex;
					},
					onLoadSuccess:function(data){
						$('#address_table').datagrid('selectRow',rowNumber);
					},
					onDblClickRow :function(rowIndex,rowData){
						//layer.msg("double click");
					  },
					/* toolbar:[{
						id:'btnedit',
						text:'修改地址',
						iconCls:'icon-edit',
						handler:function(){
							
						}
					},'-',{
						id:'decorateAudit',
						text:'删除地址',
						iconCls:'icon-remove',
						handler:function(){
							
						}
					}
					
					], */
					columns : [ [
							{
								field : 'raName',
								title : '收货人',
								width : 50,
								align : 'center'
							}, 
							{
								field : 'raTelephone',
								title : '联系方式',
								width : 50,
								align : 'center'
							},
							{
								field : 'raAddress',
								title : '收货地址',
								width : 50,
								align : 'center'
							},
							{
								field : 'raIsAcquiescent',
								title : '是否默认',
								width : 50,
								align : 'center'
							}
						   ] ]
				});
	 //设置分页控件  
		
	var p = $('#address_table').datagrid('getPager');
		$(p).pagination({
			pageSize : 10,//每页显示的记录条数，默认为10  
			pageList : [ 5, 10, 20, 30, 40, 50 ],//可以设置每页记录条数的列表  
			beforePageText : '第',//页数文本框前显示的汉字  
			afterPageText : '页    共 {pages} 页',
			displayMsg : '当前显示 {from} - {to} 条记录   共 {total} 条记录',
			onSelectPage : function(pageNumber, pageSize) {
				//layer.msg("pageNumber="+pageNumber+"pageSize="+pageSize)
				var datas = {
					raName:$('#sraName').textbox('getValue'),
					raTelephone:$('#sraTelephone').textbox('getValue'),
					raAddress:$('#sraAddress').textbox('getValue'),
					currentPage : (pageNumber - 1) * pageSize,
					pageSize : pageSize
				}
				loadAddressData(datas);
			},
			onRefresh : function(pageNumber, pageSize) {
				var datas = {
						raName:$('#sraName').textbox('getValue'),
						raTelephone:$('#sraTelephone').textbox('getValue'),
						raAddress:$('#sraAddress').textbox('getValue'),
						currentPage : (pageNumber - 1) * pageSize,
						pageSize : pageSize
					}
				loadAddressData(datas);
			}
		});
	});
	//search btn 
	$(function(){
		$("#searchAddress").click(function(){
			var pageSize=$('#address_table').datagrid('getPager').data("pagination").options.pageSize;
			var datas={
					raName:$('#sraName').textbox('getValue'),
					raTelephone:$('#sraTelephone').textbox('getValue'),
					raAddress:$('#sraAddress').textbox('getValue'),
					pageSize:pageSize
			}
			loadAddressData(datas);
		});
	});
	//reset btn
	$(function(){
		$("#resetAddress").click(function(){
			$('#sraName').textbox('setValue','');
			$('#sraTelephone').textbox('setValue','');
			$('#sraAddress').textbox('setValue','');
			var pageSize=$('#address_table').datagrid('getPager').data("pagination").options.pageSize;
			var datas={
					pageSize : pageSize,
			}
			loadAddressData(datas);
		});
	});
	//loading data
	var loadAddressData=function(datas){
		 $.ajax({
				url : 'admin/queryReceiverAddress.do',
				type : "POST",
				dataType : "json",						
				data : datas,
				success : function(param) {
						$('#address_table').datagrid('loadData',param);
				}
		});  
	}